package cn.lg.soar.boot.fc.controller;

import cn.lg.soar.boot.fc.entity.vo.CampusVo;
import cn.lg.soar.boot.fc.entity.vo.FcTreeVo;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.group.Create;
import cn.lg.soar.core.annotations.constraints.group.Update;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.boot.fc.entity.Campus;
import cn.lg.soar.boot.fc.service.CampusService;
import cn.lg.soar.mvc.annotations.Log;
import cn.lg.soar.mvc.annotations.QueryMapping;
import cn.lg.soar.mvc.enums.ControllerConst;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 校区表
 * @author lhp
 * @since 2024-12-25
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Api(tags = "校区表")
@Validated
@RestController
@RequestMapping("fc/campus")
public class CampusController extends BaseController<Campus, CampusService> {

    @Autowired
    private CampusService campusService;

    @Override
    protected Serializable getId(Campus entity) {
        return entity.getId();
    }

    @Log(value = "新增校区表", data = "#p0", dataId = "#$r.data")
    @IdempotentByParam("#p0")
    @ApiOperation(value = ControllerConst.CREATE_NAME)
    @RequestMapping(path = "create", method = RequestMethod.POST)
    public HttpResult<Serializable> create(@RequestBody @Validated(Create.class) Campus data) {
        return super.createResult(data);
    }

    @Log(value = "修改校区表", data = "#p0", dataId = "#p0.id")
    @ApiOperation(value = ControllerConst.UPDATE_NAME)
    @RequestMapping(path = "update", method = RequestMethod.POST)
    public HttpResult<Void> update(@RequestBody @Validated(Update.class) Campus data) {
        return super.updateResult(data);
    }

    @Log(value = "删除校区表", data = "#p0")
    @ApiOperation(value = ControllerConst.REMOVE_NAME, notes = "根据 id 删除")
    @RequestMapping(path = "remove", method = RequestMethod.POST)
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        return super.removeResult(ids);
    }

    @ApiOperation(value = ControllerConst.DETAIL_NAME, notes = "根据 ID 获取一条数据")
    @RequestMapping(path = "detail", method = RequestMethod.GET)
    public HttpResult<Campus> detail(@RequestParam Long id) {
        return super.detailResult(id);
    }

    @ApiOperation(value = ControllerConst.QUERY_NAME, notes = "多功能查询（分页、列表、统计）")
    @RequestMapping(path = "query", method = RequestMethod.GET)
    @QueryMapping
    public HttpResult<PageResult<Campus>> query(PageQuery pageQuery, QueryParams params){
        return super.queryResult(pageQuery, params);
    }

    @ApiOperation(value = ControllerConst.QUERY_NAME, notes = "查询全部校区")
    @RequestMapping(path = "allList", method = RequestMethod.GET)
    @GetMapping
    public HttpResult<List<CampusVo>> getAllList(){
        return HttpResult.data(campusService.getListVo());
    }

    @ApiOperation(value = ControllerConst.QUERY_NAME, notes = "树状菜单")
    @RequestMapping(path = "tree", method = RequestMethod.GET)
    @QueryMapping
    public HttpResult<PageResult<FcTreeVo>> tree(PageQuery pageQuery, QueryParams params){
        return HttpResult.data(campusService.queryResultVo(pageQuery, params));
    }

    @ApiOperation(value = ControllerConst.QUERY_NAME, notes = "树状菜单")
    @RequestMapping(path = "firstTree", method = RequestMethod.GET)
    @QueryMapping
    public HttpResult<PageResult<FcTreeVo>> firstTree(PageQuery pageQuery, QueryParams params){
        return HttpResult.data(campusService.queryFirstTreeVo(pageQuery, params));
    }

    @ApiOperation(value = ControllerConst.QUERY_NAME, notes = "树状菜单")
    @RequestMapping(path = "easyTree", method = RequestMethod.GET)
    @QueryMapping
    public HttpResult<PageResult<JSONObject>> easyTree(PageQuery pageQuery, QueryParams params){
        return HttpResult.data(campusService.queryResultEasyVo(pageQuery, params));
    }

    @Log(value = "导出校区表")
    @ApiOperation(value = ControllerConst.EXPORT_NAME)
    @RequestMapping(path = "export", method = RequestMethod.GET)
    public void exports(String filename, QueryParams params, HttpServletResponse resp){
        campusService.exportExcel(filename, params, resp);
    }

    @Log(value = "导入校区表")
    @ApiOperation(value = ControllerConst.IMPORT_NAME)
    @RequestMapping(path = "import", method = RequestMethod.POST)
    public HttpResult<Void> imports(@RequestParam MultipartFile files){
        return campusService.importExcel(files);
    }

    @Log(value = "拖拽排序")
    @ApiOperation(value = ControllerConst.UPDATE_NAME)
    @RequestMapping(path = "updateSort", method = RequestMethod.POST)
    public HttpResult<Serializable> updateSort(@RequestBody List<Map<String, Integer>> sortedData) {
        return campusService.updateSort(sortedData);
    }
}